package com.example.demo.util;

import com.example.demo.controller.LoginExitController;
import com.example.demo.enity.Bill;
import com.example.demo.enity.Provider;
import com.example.demo.service.ProviderListService;
import com.example.demo.service.SupperService;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 工具类
 * 作用：
 * 1.Date类型---->String类型
 * 2.String类型---->Date类型
 */

public class Tools {


    /**
     * Date类型---->String类型
     *
     * @param date 类型
     * @return String 类型
     */
    public static String date_string(Date date) {
        //字符串的格式要根据情况进行定义 例如："yyyy年MM月dd日hh时mm分"
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
        String format = simpleDateFormat.format(date);
        return format;
    }


    /**
     * String类型---->Date类型
     *
     * @param date 类型
     * @return Date 类型
     */
    public static Date string_date(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
        try {
            Date parse = simpleDateFormat.parse(date);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 关于billList请求的方法整合
     *
     * @param model
     * @param providerListService
     * @param bill
     */
    public static void billListMethod(Model model,
                                      ProviderListService providerListService,
                                      Bill bill) {

        //将动态的用户登录名显示在登录页面
        String username = LoginExitController.username;
        model.addAttribute("username", username);

        //查询出数据库中所有的供应商信息
        List<Provider> providerList = providerListService.getList(null);
        model.addAttribute("providerList", providerList);

        //将查询条件（商品名称）放入到页面层
        String goodsName = bill.getGoodsName();
        model.addAttribute("goodsName", goodsName);

        //将查询条件（支付状态）放入到页面层
        Integer isPayment = bill.getIsPayment();
        model.addAttribute("isPayment", isPayment);

        /**
         * 由于实体类中的属性（供应商）是字符串 但是在下拉菜单中提供的是供应商的id
         * 所以将Integer类型的数字根据查询的结果>>=String类型的字符
         */
        String supplierId = bill.getSupplier();
        try {
            Integer supplierCode = Integer.valueOf(supplierId);
            model.addAttribute("supplierCode", supplierCode);
            Provider provider = providerListService.findById(supplierCode);
            bill.setSupplier(provider.getSupplierName());
        } catch (Exception e) {
            bill.setSupplier(null);
        }
    }


    /**
     * 关于添加整合方法
     */
    public static String addObject(Object object,
                                   Model model,
                                   SupperService supperService,
                                   String success,
                                   String fail) {
        String address = "";

        Integer integer = 0;
        try {
            int i = supperService.addObject(object);
            integer = i;
        } catch (Exception e) {
            System.out.println();
            System.out.println();
            System.out.println("错误信息为：" + e.getMessage() + "!");
            System.out.println();
            System.out.println();
            model.addAttribute("message", "添加失败!");
        }
        if (integer == 1) {
            address = "redirect:/" + success;
        } else {
            address = fail;
        }
        return address;
    }


    public static String updateObject(Object object,
                                      Model model,
                                      SupperService supperService,
                                      String success,
                                      String fail) {
        String address = "";

        Integer integer = 0;
        try {
            int i = supperService.updateObject(object);
            integer = i;
        } catch (Exception e) {
            System.out.println();
            System.out.println();
            System.out.println("错误信息为：" + e.getMessage() + "!");
            System.out.println();
            System.out.println();
            model.addAttribute("message", "修改失败!");
        }
        if (integer == 1) {
            address = "redirect:/" + success;
        } else {
            address = fail;
        }
        return address;
    }


    public static String updateObject(Object object,
                                      Model model,
                                      String fail,
                                      SupperService supperService) {

        String address = null;

        Integer integer = 0;
        try {
            int i = supperService.updateObject(object);
            integer = i;
        } catch (Exception e) {
            System.out.println();
            System.out.println();
            System.out.println("错误信息为：" + e.getMessage() + "!");
            System.out.println();
            System.out.println();
            model.addAttribute("message", "修改失败!");
        }
        if (integer == 0) {
            address = fail;
        }
        return address;
    }


    /**
     * 在使用Test进行对方法的运行时该方法必须是void 不能拥有返回值 而且方法不能还有参数
     */


    /**
     * 测试
     */
    @Test
    public void test_string_date() {
        Date date = string_date("2021年12月07日19时12分");
        System.out.println(date);
    }


    /**
     * 测试
     */
    @Test
    public void test_date_string() {
        String string = date_string(new Date());
        System.out.println(string);
    }


}
